package com.cdu.controller;

import com.cdu.annotation.OperationLogAnnotation;
import com.cdu.enums.MessageEnum;
import com.cdu.enums.OperationLogEnum;
import com.cdu.model.common.BaseResponse;
import com.cdu.model.query.TaskQuery;
import com.cdu.service.TaskService;
import com.cdu.util.ParamUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;

/**
 * projectName : easy-help-v2
 *
 * @author :lql
 * date: 2022 -05 -23
 */
@RestController
@RequestMapping("/t")
@Api(value = "task", tags = "task - controller")
@Slf4j
@CrossOrigin
public class TaskController {
    @Autowired
    private TaskService taskService;

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "Authentication", required = true),
    })
    @PostMapping("/queryAllTask")
    @ApiOperation("查询任务接口")
    @OperationLogAnnotation(getOperationAction = OperationLogEnum.QUERY)
    public BaseResponse queryAllTask(HttpServletRequest request, TaskQuery taskQuery) {
        return taskService.queryAllTask(taskQuery);
    }


    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "查询任务id", dataType = "String", required = true, dataTypeClass = String.class)
    )
    @GetMapping("/queryTaskById/{id}")
    @ApiOperation("通过id查询任务接口")
    @OperationLogAnnotation(getOperationAction = OperationLogEnum.QUERY)
    public BaseResponse queryTaskById(HttpServletRequest request, @PathVariable(required = false) String id) {
        TaskQuery taskQuery = new TaskQuery();
        taskQuery.setId(id);
        return taskService.queryTaskById(taskQuery);
    }


    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "Authentication", required = true),
    })
    @PostMapping("/create")
    @ApiOperation("创建任务接口")
    @OperationLogAnnotation(getOperationAction = OperationLogEnum.CREATE)
    public BaseResponse create(HttpServletRequest request) throws ParseException {
        JSONObject ido = JSONObject.fromObject(ParamUtil.getAllParamMap(request));
        return taskService.createTask(ido);
    }


    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "Authentication", required = true),
    })
    @PostMapping("/update")
    @ApiOperation("修改任务接口")
    @OperationLogAnnotation(getOperationAction = OperationLogEnum.UPDATE)
    public BaseResponse update(HttpServletRequest request) throws ParseException {
        JSONObject ido = JSONObject.fromObject(ParamUtil.getAllParamMap(request));
        return taskService.updateTask(ido);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "Authentication", required = true),
    })
    @PostMapping("/accept")
    @ApiOperation("接受任务接口")
    @OperationLogAnnotation(getOperationAction = OperationLogEnum.ACCEPT)
    public BaseResponse register(HttpServletRequest request) throws ParseException {
        JSONObject ido = JSONObject.fromObject(ParamUtil.getAllParamMap(request));
        ido.put("uid", request.getAttribute("userId"));
        return taskService.acceptTask(ido);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "Authentication", required = true),
    })
    @PostMapping("/deal")
    @ApiOperation("接受任务接口")
    @OperationLogAnnotation(getOperationAction = OperationLogEnum.ACCEPT)
    public BaseResponse deal(HttpServletRequest request) throws ParseException {
        JSONObject ido = JSONObject.fromObject(ParamUtil.getAllParamMap(request));
        return taskService.dealTask(ido);
    }


    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "删除任务id", paramType = "path", dataType = "String", required = true, dataTypeClass = String.class)
    )
    @GetMapping("/delTask/{id}")
    @ApiOperation("删除任务接口")
    @OperationLogAnnotation(getOperationAction = OperationLogEnum.DELETE)
    public BaseResponse delTask(HttpServletRequest request, @PathVariable(required = false) String id) {
        TaskQuery taskQuery = new TaskQuery();
        taskQuery.setId(id);
        return taskService.delTaskById(taskQuery);
    }


    @DeleteMapping("/del")
    @ApiOperation("通过ids数组删除所有包含的在内的任务")
    @OperationLogAnnotation(getOperationAction = OperationLogEnum.DELETE)
    @ApiImplicitParams(
            @ApiImplicitParam(name = "ids", value = "需要删除的id数组", paramType = "body", dataType = "List", required = true, dataTypeClass = List.class)
    )
    public BaseResponse delByIds(HttpServletRequest request) {
        JSONObject ido = JSONObject.fromObject(ParamUtil.getAllParamMap(request));
        if (!ido.containsKey("ids") || ido.get("ids") == null) {
            return BaseResponse.response(MessageEnum.PARAM_INVALID);
        }
        List<String> ids = (List<String>) ido.get("ids");
        return BaseResponse.response(taskService.removeBatchByIds(ids), MessageEnum.SUCCESS);
    }
}
